<template>
  <div>
    <el-card shadow="never">
      <h3>用户管理</h3>
    </el-card>

    <br>

    <data-table :table-data="tableData">
      <el-table-column
        label="编号"
        align="center"
        width="80"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="头像"
        align="center"
      >
        <template slot-scope="scope">
          <img width="75" height="75" :src="scope.row.avatar" alt="" style="border-radius:50%;">
        </template>
      </el-table-column>
      <el-table-column
        label="账号"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="用户角色"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.roles==='1'" size="mini" type="primary">普通用户</el-tag>
          <el-tag v-if="scope.row.roles==='2'" size="mini" type="danger">管理员</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="昵称"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.nickName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="邮箱"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.email }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="电话"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="注册时间"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | formatDate2 }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="primary" plain @click="editBtn(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" plain @click="delBtn(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </data-table>

    <!-- 编辑模态框 -->
    <el-dialog
      title="编辑"
      :visible.sync="editVisible"
      width="40%"
    >
      <el-form label-width="80px" size="mini">
        <el-form-item label="头像">
          <el-upload
            class="avatar-uploader"
            action="/api/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.avatar " :src="form.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="账号">
          <span>{{ form.username }}</span>
        </el-form-item>
        <el-form-item label="用户角色">
          <el-select v-model="form.roles">
            <el-option label="普通用户" value="1" />
            <el-option label="管理员" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" clearable type="password" />
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickName" clearable />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" clearable />
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.phone" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="confirmEditBtn">更新</el-button>
          <el-button plain @click="editVisible=false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import { getAllUser, updateUser, delUser } from '@/api/user'
import DataTable from '@/components/DataTable'
export default {
  name: 'UserManage',
  components: {
    DataTable
  },
  data() {
    return {
      addVisible: false,
      editVisible: false,
      form: {},
      tableData: [],
      imageUrl: ''
    }
  },
  created() {
    this.getData()
  },
  methods: {
    getData() {
      getAllUser().then(resp => {
        this.tableData = resp.data
      })
    },
    addBtn() {
      this.form = {}
      this.addVisible = true
    },
    editBtn(row) {
      this.form = { ...row }
      this.editVisible = true
    },
    confirmEditBtn() {
      updateUser(this.form).then(resp => {
        this.$notifySuccess(resp.msg)
        this.editVisible = false
        this.getData()
      })
    },

    delBtn(id) {
      this.$confirm('确认要删除吗？', '提示', {
        type: 'warning',
        confirmButtonText: '确认',
        cancelButtonText: '取消'
      }).then(() => {
        delUser(id).then(resp => {
          this.$notifySuccess(resp.msg)
          this.getData()
        })
      })
    },

    beforeAvatarUpload(file) {
      const isType = file.type === 'image/jpeg' || file.type === 'image/png'
      if (!isType) {
        this.$message.error('上传头像图片只能是 jpg 或者 png 格式!')
      }
      return isType
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.form.avatar = res.data
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 50%;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>
